<template>
	<view class="content">
		<view class="uni-flex uni-column" style="justify-content: center; align-items: center;">
			<image class="uni-logo" src="http://img.deitui.com/?w=200&h=200&text=Logo&fontsize=36&textclor=fff&bgclor=ed6d53"></image>
		</view>
		<view class="uni-cell">
			<view class="uni-cell__value">
				<input type="number" v-model="username" placeholder="请输入手机号码">
			</view>
			<view class="uni-icon uni-icon-close1 uni-font-size-28  uni-padding-top-15 uni-padding-bottom-15 " style="font-size: :44upx;" custom-class="uni-cell__right-icon"  v-if="username.length>0" @click="clearPhone"></view>

		</view>
		<view class="uni-cell">
			<view class="uni-cell__value">
				<input type="password"  v-model="password" placeholder="请输入验证码" v-if="passwordType == 'password'" >
				<input type="text"  v-model="password" placeholder="请输入验证码" v-if="passwordType == 'text'" >
			</view>
			<view class="uni-icon uni-icon-eye uni-text-gray   uni-padding-top-15 uni-padding-bottom-15 " custom-class="uni-cell__right-icon" style="font-size: :32upx;" v-if="passwordType == 'text'" @click="changePasswordType"></view>
			<view class="uni-icon uni-icon-eye-close uni-text-gray   uni-padding-top-15 uni-padding-bottom-15 " style="font-size: :32upx;" custom-class="uni-cell__right-icon" v-if="passwordType == 'password'" @click="changePasswordType"></view>
			<view class="uni-icon uni-icon-close1 uni-text-gray  uni-padding-top-15 uni-padding-bottom-15  "  custom-class="uni-cell__right-icon"  v-if="password.length>0" @click="clearPassword" ></view>
		</view>
		<view class="btn-row">
			<button type="primary" class="uni-bg-default" @tap="bindLogin">登录</button>
		</view>
		<view class="action-row">
			<view @tap="goTo(0)">注册账号</view>
			<text>|</text>
			<view @tap="goTo(1)">忘记密码</view>
		</view>
		<!--      <view class="oauth-row" v-if="hasProvider" v-bind:style="{top: positionTop + 'px'}">
            <view class="oauth-image" v-for="provider in providerList" :key="provider.value">
                <image :src="provider.image" @tap="oauth(provider.value)"></image>
            </view>
        </view> -->
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'

	export default {
		data() {
			return {
				pages: [
					"/pages/security/registery",
					"/pages/security/loginValidate"
				],
				providerList: [],
				hasProvider: false,
				isClear: false,
				passwordType: 'password',
				username: '',
				password: '',
				positionTop: 0,
				delta: 1,
			}
		},
		computed: mapState(['forcedLogin']),
		onLoad(e) {
			if(e.delta !== undefined)
				this.delta = e.delta + this.delta;
		},
		methods: {
			goTo(index) {
				let delta = this.delta + 1;
				var that = this;
				uni.navigateTo({
					url: that.pages[index]+"?delta="+ this.delta
				})
			},
			initPosition() {
				/**
				 * 使用 absolute 定位，并且设置 bottom 值进行定位。软键盘弹出时，底部会因为窗口变化而被顶上来。
				 * 反向使用 top 进行定位，可以避免此问题。
				 */
				this.positionTop = uni.getSystemInfoSync().windowHeight - 100;
			},
			bindLogin() {
				/**
				 * 客户端对账号信息进行一些必要的校验。
				 * 实际开发中，根据业务需要进行处理，这里仅做示例。
				 */
				if (this.username.length < 5) {
					uni.showToast({
						icon: 'none',
						title: '账号最短为 5 个字符'
					});
					return;
				}
				if (this.password.length < 6) {
					uni.showToast({
						icon: 'none',
						title: '密码最短为 6 个字符'
					});
					return;
				}
				/**
				 * 下面简单模拟下服务端的处理
				 * 检测用户账号密码是否在已注册的用户列表中
				 * 实际开发中，使用 uni.request 将账号信息发送至服务端，客户端在回调函数中获取结果信息。
				 */
				const data = {
					username: this.username,
					password: this.password
				};
				uni.showLoading({
					title:"登录中......"
				})
				this.$store.dispatch("LoginByUsername", data).then(response => {
					this.$store.dispatch("connect", {userId:this.$store.state.user.userId, access_token:this.$store.state.user.access_token})
					.then(response => {
							this.toMain(this.username);
					}).catch(err => {
						uni.showModal({
							title:"提示",
							content:"登录失败,请重试，谢谢"
						})
					})
					
				}).catch(err => {
					uni.showModal({
						title:"提示",
						content:"登录失败,请重试，谢谢"
					})
				});
			},
			clearPhone() {
				this.username = "";
			},
			clearPassword() {
				this.password = "";
			},
			changePasswordType: function() {
				this.passwordType=(this.passwordType=='password'?'text':'password')
			},
			toMain(userName) {
				this.$store.dispatch("GetUserInfo");
				//this.login(userName);
				if (this.forcedLogin) {
					console.log("reLaunch")
					uni.reLaunch({
						url: '../main/main',
					});
				} else {
					console.log("navigateBack")
					uni.navigateBack({
						delta: this.delta
					})
				}

			}
		},
		onLoad() {
			this.initPosition();
		}
	}
</script>

<style>


	.action-row {
		display: flex;
		flex-direction: row;
		justify-content: center;
	}


	.action-row {
		display: flex;
		flex-direction: row;
		justify-content: center;
	}

	.action-row navigator {
		color: #7C4BF9;
		padding: 0 20px;
	}
</style>
